using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Java.Interop;
using Org.Json;
using TMM.Common;

namespace TMM
{
    public class JS2CS : Java.Lang.Object, Java.Lang.IRunnable
    {
        Context context;
        MainActivity mainActivity = null;

        public JS2CS(Context context)
        {
            this.context = context;
            mainActivity = context as MainActivity;
        }

        public void Run()
        {
        }

        [Export("getPicture")]
        public void getPicture(string titleText, string cameraText, string galleryText, string cancelText)
        {
            mainActivity.RunOnUiThread(() =>
            {
                mainActivity.GetPicture(titleText, cameraText, galleryText, cancelText);
            });
        }

        [Export("getPictureData")]
        public string getPictureData()
        {
            if (mainActivity.PictureData == null)
                return null;

            var obj = new JSONObject();
            obj.Put("pic", mainActivity.PictureData.Data);
            obj.Put("name", mainActivity.PictureData.Name);

            return obj.ToString();
        }

        [Export("getDeviceId")]
        public string getDeviceId()
        {
            return DeviceInfo.Id;
        }

        [Export("hideSplashscreen")]
        public void hideSplashscreen()
        {
            mainActivity.RunOnUiThread(() =>
            {
                mainActivity.HideSplashScreen();
            });
        }

        [Export("showSplashscreen")]
        public void showSplashscreen()
        {
            mainActivity.RunOnUiThread(() =>
            {
                mainActivity.ShowSplashScreen();
            });
        }

        [Export("setPageTitle")]
        public void setPageTitle(string title)
        {
            mainActivity.RunOnUiThread(() =>
            {
                mainActivity.SetTitle(title);
            });
        }

        [Export("showMenu")]
        public void showMenu()
        {
            mainActivity.RunOnUiThread(() =>
            {
                mainActivity.ShowMenu();
            });
        }

        [Export("hideMenu")]
        public void hideMenu()
        {
            mainActivity.RunOnUiThread(() =>
            {
                mainActivity.HideMenu();
            });
        }

        [Export("exitApplication")]
        public void exitApplication()
        {
            mainActivity.RunOnUiThread(() =>
            {
                mainActivity.Finish();
            });
        }

        public void setUserSettings(string data)
        {
            var authData = context.GetSharedPreferences("userSettings", FileCreationMode.Private);
            var editor = authData.Edit();
            editor.PutBoolean("HasData", true);
            editor.PutString("UserSettings", data);
            editor.Commit();
        }

        [Export("getUserSettings")]
        public string getUserSettings()
        {
            var authData = context.GetSharedPreferences("userSettings", FileCreationMode.Private);
            if (authData != null && authData.GetBoolean("HasData", false))
                return authData.GetString("UserSettings", string.Empty);

            return string.Empty;
        }

        [Export("getAuthData")]
        public string getAuthData()
        {
            var authData = context.GetSharedPreferences("authData", FileCreationMode.Private);
            if (authData != null && authData.GetBoolean("HasData", false))
                return authData.GetString("AuthData", string.Empty);

            return string.Empty;
        }

        [Export("setAuthData")]
        public void setAuthData(string data)
        {
            var authData = context.GetSharedPreferences("authData", FileCreationMode.Private);
            var editor = authData.Edit();
            editor.PutBoolean("HasData", true);
            editor.PutString("AuthData", data);
            editor.Commit();
        }

        [Export("deleteAuthData")]
        public void deleteAuthData()
        {
            var authData = context.GetSharedPreferences("authData", FileCreationMode.Private);
            var editor = authData.Edit();
            editor.PutBoolean("HasData", false);
            editor.Commit();
        }

        [Export("setModuleAction")]
        public void setModuleAction(string action)
        {
            mainActivity.RunOnUiThread(() =>
            {
                mainActivity.SetAction(action);
            });
        }

        [Export("setNotifications")]
        public void setNotifications(int nr)
        {
            mainActivity.RunOnUiThread(() =>
            {
                mainActivity.SetNumberOfNotifications(nr);
            });
        }

        [Export("updateApplication")]
        public void updateApplication(string appStoreUrl)
        {
            mainActivity.RunOnUiThread(() =>
            {
                mainActivity.ShowUpdateApplicationPopup(appStoreUrl, mainActivity);
            });
        }

        [Export("disconnect")]
        public void disconnect()
        {
            mainActivity.RunOnUiThread(() =>
            {
                mainActivity.ReloadWebView();
            });
        }

        [Export("setServer")]
        public void setServer(string serverCode)
        {
            var serverPreferences = context.GetSharedPreferences("Server", FileCreationMode.Private);
            var editor = serverPreferences.Edit();
            editor.PutString("Code", serverCode);
            editor.Commit();
        }

        [Export("loadApplication")]
        public void loadApplication(string serverCode, string username, string password)
        {
            mainActivity.RunOnUiThread(() =>
            {
                if (!string.IsNullOrWhiteSpace(username) && !string.IsNullOrWhiteSpace(password))
                    new LocalData(mainActivity).SetAuthenticationData(new AuthenticationData()
                    {
                        ServerCode = serverCode.ToLower().Trim(),
                        Username = username.Trim(),
                        Password = password.Trim()
                    });

                mainActivity.LoadApplication(serverCode);
            });
        }

        [Export("getConnectionData")]
        public void getConnectionData()
        {
            mainActivity.RunOnUiThread(() =>
            {
                mainActivity.Authenticate();
            });
        }
    }
}